package bizapi.sinosoft.com.config.oauth;

import jd.restful.model.User;
import jd.restful.repository.UserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by xujingfeng on 2017/2/15.
 */
@Service
public class UserService {
    @Autowired
    UserDao userDao;

    public User findByAccount(String account) {
        User user = null;
        boolean isMobile = isMobile(account);
        if (isMobile) {
            user = userDao.findByMobile(account);
            return user;
        }
        boolean isEmail = isEmail(account);
        if (isEmail) {
            user = userDao.findByEmail(account);
            return user;
        } else {
            user = userDao.findByUsername(account);
            return user;
        }
    }

    public static boolean isMobile(String mobile) {
        Pattern p = Pattern.compile("^1[3|4|5|7|8]\\d{9}$");
        Matcher m = p.matcher(mobile);
        return m.matches();
    }

    public static boolean isEmail(String email) {
        boolean matchEmail = Pattern.matches("\\w+((\\.|\\-|\\+)\\w+)*@([\\w\\-]+\\.)+(\\w{2,4})+", email);
        return matchEmail;
    }
}
